<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Tool\Helper;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;


class UsersController extends Controller {
    // 获取自己的信息 - 刷新数据用
    public function info()
    {
        $info = User::whereId(auth('api')->id())
            ->first([
                'avatar', 'sex', 'mobile',
                'nickname', 'age', 'height',
                'weight'
            ]);
        return $this->writeJson(0, $info);
    }

    // 更新资料
    public function update(Request $request)
    {
        $info = User::find(auth('api')->id());
        $info->avatar = $request->post('avatar');
        $info->sex = $request->post('sex');
        $info->nickname = $request->post('nickname');
        $info->age = $request->post('age');
        $info->height = $request->post('height');
        $info->weight = $request->post('weight');
        $info->save();
        return $this->writeJson(00, null, '保存成功');
    }

    // 生成并获取用户的海报
    public function shareImg()
    {
        $uid = auth('api')->id();
        $content = "https://api.panhohealth.cn/api/share/?uid={$uid}";
        $path = Helper::makeQrCode($content, 100);
        if (empty($path)) {
            return $this->writeJson(500,null,'获取失败');
        }
        Image::make(storage_path() . '/attachment/share.png')
            ->resize(471, 952)
            ->insert(public_path($path), 'bottom-left', 175, 105)
            ->save(public_path($path));

        return $this->writeJson(0,[
            'path' => config('app.url') . $path
        ],'获取成功');
    }

    public function share()
    {
        echo '36522b3d1a05c1805c895b474710e6f5';
    }
}
